<template>
  <BasicTable @register="registerTable">
    <template #action="{ record }">
      <TableAction
        :actions="[
          {
            icon: 'clarity:note-edit-line',
            tooltip: '编辑',
            onClick: handleEdit.bind(null, record),
          },
          {
            icon: 'ant-design:eye-invisible-outlined',
            tooltip: '隐藏',
            popConfirm: {
              title: '是否确认隐藏',
              confirm: handleHide.bind(null, record),
            },
          },
          {
            icon: 'ant-design:delete-outlined',
            tooltip: '删除',
            color: 'error',
            popConfirm: {
              title: '是否确认删除',
              confirm: handleDel.bind(null, record),
            },
          },
        ]"
      />
    </template>
  </BasicTable>
</template>
<script lang="ts">
  import { defineComponent, watch, nextTick } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { columns } from './configManagementTable.data';
  import {
    getConfigList,
    getConfigUpdateConfig,
    getConfigHideElement,
  } from '/@/api/flowManage/flowManage';
  import { useMessage } from '/@/hooks/web/useMessage';
  export default defineComponent({
    name: 'ConfigManagementTable',
    components: { BasicTable, TableAction },
    props: {
      activeKey: {
        type: String,
        default: '',
      },
    },
    setup(props) {
      const { createMessage } = useMessage();
      watch(
        () => props.activeKey,
        async () => {
          await nextTick();
          reload();
        },
        {
          immediate: true,
        },
      );
      const [registerTable, { reload, setProps }] = useTable({
        title: '',
        api: getConfigList,
        rowKey: 'id',
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: [],
          autoSubmitOnEnter: true,
        },
        useSearchForm: false,
        showTableSetting: false,
        bordered: true,
        // handleSearchInfoFn(info) {
        //   console.log('handleSearchInfoFn', info);
        //   return info;
        // },
        afterFetch: async (data) => {
          if (props.activeKey) {
            const newData = data.filter((item) => item.kind == props.activeKey);
            return newData;
          }
        },
        showHeader: false,
        scroll: { y: window?.innerHeight - 260 },
        pagination: false,
        immediate: false,
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });

      const handleEdit = async (record) => {
        try {
          handleSetProps(true);
          await getConfigUpdateConfig(record);
          createMessage.success('操作成功');
          reload();
        } finally {
          handleSetProps(false);
        }
      };

      const handleHide = async (record) => {
        try {
          handleSetProps(true);
          await getConfigHideElement(record);
          createMessage.success('操作成功');
          reload();
        } finally {
          handleSetProps(false);
        }
      };

      const handleDel = async (record) => {
        try {
          handleSetProps(true);
          await getConfigHideElement(record);
          createMessage.success('操作成功');
          reload();
        } finally {
          handleSetProps(false);
        }
      };

      const handleSetProps = (bool) => {
        setProps({ loading: bool });
      };

      return {
        registerTable,
        handleEdit,
        handleHide,
        handleDel,
      };
    },
  });
</script>
